package me.aurora.service.impl;

import me.aurora.domain.Menu;
import me.aurora.domain.Role;
import me.aurora.domain.vo.MenuVo;
import me.aurora.repository.MenuRepo;
import me.aurora.service.MenuService;
import me.aurora.service.dto.MenuDTO;
import me.aurora.service.dto.RoleDTO;
import me.aurora.service.mapper.MenuMapper;
import me.aurora.util.ListSortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 郑杰
 * @date 2018/08/23 17:27:57
 */
@Service
public class MenuServiceIMpl implements MenuService {

    @Autowired
    private MenuRepo menuRepo;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<MenuVo> buildMenuUrl(List<MenuDTO> menuList) {
        List<MenuVo> menuVoList = new ArrayList<>();
        for (MenuDTO menu: menuList) {
            if(menu.getLevel()!=1){
                MenuVo menuVo = new MenuVo();
                menuVo.setPath(menu.getUrl());
                menuVo.setComponent(menu.getUrl());
                menuVo.setName(menu.getName());
                menuVoList.add(menuVo);
            }
        }
        return menuVoList;
    }

    @Override
    public List<MenuDTO> findMenusByUserRols(Set<Role> roles) {
        Set<Menu> menuSet = new HashSet<>();
        for (Role role : roles) {
            menuSet.addAll(role.getMenus());
        }
        List<Menu> menus = new ArrayList<>();
        menus.addAll(menuSet);
        List<MenuDTO> menuDTOS = menuMapper.toDto(menus);
        ListSortUtil<MenuDTO> sortList = new ListSortUtil<MenuDTO>();
        sortList.sort(menuDTOS, "soft", "asc");
        return menuDTOS;
    }
}
